﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using WiseLink.RDS.Resource;

namespace CCF20Model
{
    partial class Vehicle : IDataErrorInfo
    {
        CCF20Entities ccf20Ent;
        private int _urgentCount;
        
        public global::System.Int32 UrgentCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                _urgentCount = ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == WiseLinkRDSStatus.Fault_Type_Urgent && f.VehicleID == this.VehicleID).Count();
                return _urgentCount;
            }
            set
            {
                if (_urgentCount != value)
                {
                    _urgentCount = value;
                }
            }

        }

        public global::System.Int32 ConfirmedFaultCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == WiseLinkRDSStatus.Fault_Type_Fault && 
                                                    f.VehicleID == this.VehicleID && 
                                                    f.IsConfirmed &&
                                                    f.Priority<3).Count();
            }

        }
        public global::System.Int32 FaultCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == WiseLinkRDSStatus.Fault_Type_Fault && f.VehicleID == this.VehicleID).Count();
            }

        }

        public global::System.Int32 UnConfirmedFaultCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.Faults.Where(f => f.IsActive && f.FaultType == WiseLinkRDSStatus.Fault_Type_Fault && f.VehicleID == this.VehicleID && !f.IsConfirmed).Count();
            }

        }
        public global::System.Int32 MaintenanceCount
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                return ccf20Ent.VehicleMaintenanceHistories.Where(f => f.IsActive && f.VehicleID == this.VehicleID).Count();
            }

        }
        string _SMSStatus = "未发送";
        public global::System.String SMSStatus
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                int count = ccf20Ent.SMSHistories.Where(s => s.IsActive && s.VehicleID == this.VehicleID).Count();
                if (count > 0)
                    _SMSStatus = "已发送";
                return _SMSStatus;
            }

        }

        public Vehicle()
        {
            this.SaleDate = DateTime.Now;
            ccf20Ent = new CCF20Entities();

            //ccf20Ent.Faults.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
        }


        partial void OnVehicleTypeIDChanged()
        {
            this.RemoveError("VehicleTypeID");
            if ((this.VehicleTypeID == null) || (this.VehicleTypeID == 0))
            {
                this.AddError("VehicleTypeID", "车辆型号不可为空");
            }
        }
        partial void OnVehicleLicenseChanged()
        {
            this.RemoveError("VehicleLicense");

            if (!string.IsNullOrEmpty(this.VehicleLicense))
            {
                var count = from v in ccf20Ent.Vehicles
                            where v.VehicleLicense.Equals(this.VehicleLicense) && v.VehicleID != this.VehicleID
                            select v;


                if (count.Count() > 0)
                    this.AddError("VehicleLicense", "车牌号不可重复");
            }
        }
        /*
        partial void OnChassiNoChanged()
        {
            this.RemoveError("ChassiNo");

            if (string.IsNullOrEmpty(this.ChassiNo))
            {
                this.AddError("ChassiNo", "底盘号不可为空");

            }
            else if (this.ChassiNo.Length != 17)
            {
                this.AddError("ChassiNo", "底盘号必须是17位");
            }
            else
            {
                var count = from v in ccf20Ent.Vehicles
                            where v.ChassiNo.Equals(this.ChassiNo) && v.VehicleID != this.VehicleID
                            select v;


                if (count.Count() > 0)
                    this.AddError("ChassiNo", "底盘号不可重复");
            }
        }

        partial void OnEngineNoChanged()
        {
            this.RemoveError("EngineNo");

            if (string.IsNullOrEmpty(this.EngineNo))
            {
                this.AddError("EngineNo", "引擎号不可为空");

            }
            else
            {
                var count = from v in ccf20Ent.Vehicles
                            where v.EngineNo.Equals(this.EngineNo) && v.VehicleID != this.VehicleID
                            select v;


                if (count.Count() > 0)
                    this.AddError("EngineNo", "引擎号不可重复");
            }
        }


        partial void OnSaleDateChanged()
        {
            this.RemoveError("SaleDate");
            
            if (SaleDate == null)
            {
                this.AddError("SaleDate", "销售日期不可为空");

            }
        }

        partial void OnCurrentMileageChanged()
        {
            this.RemoveError("CurrentMileage");
            if (CurrentMileage == null)
            {
                this.AddError("CurrentMileage", "当前里程不可为空");

            }
        }*/
        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion

    }
}
